// @npc用的接口
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');

const User = require('../../models/User/User');
const Maps = require('../../models/Map');
const Npc = require('../../models/Npc/Npc');
const open_api = require('../../controller/open_api');

// @route  Post api/npcs/add
// @desc   新增npc
// @access public
router.post('/add', (req, res) => {
    Npc.findOne({ name: req.body.name }).then(npc => {
        if (npc) {
          return res.status(400).json('该NPC已存在');
        } else {
            const NpcMap = {};
            if(req.body.name){NpcMap.name = req.body.name};
            if(req.body.identity){NpcMap.identity = req.body.identity};
            // if(req.body.now_map){NpcMap.now_map = req.body.now_map};
            NpcMap.now_map = '5ff57a5926915d66c05a0357';//先写在主城里
            if(req.body.strength){NpcMap.strength = req.body.strength};
            if(req.body.defense){NpcMap.defense = req.body.defense};
            if(req.body.magic){NpcMap.magic = req.body.magic};
            if(req.body.speed){NpcMap.speed = req.body.speed};
            if(req.body.user_exp){NpcMap.user_exp = req.body.user_exp};
            if(req.body.hp){NpcMap.hp = req.body.hp};
            if(req.body.max_hp){NpcMap.max_hp = req.body.max_hp};

            new Npc(NpcMap).save()
            .then(npc => res.json(npc))
            .catch(err => console.log(err));
        }
    });
  });

module.exports = router;
  